package com.yuanjiu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yuanjiu.common.util.Global;
import com.yuanjiu.pojo.yuanjiu.CategoryIdName;
import com.yuanjiu.pojo.yuanjiu.JiuObject;
import com.yuanjiu.service.yuanjiu.HomeService;
import com.yuanjiu.vo.yuanjiu.ProductVo;

@Controller
@RequestMapping("/")
public class HomeController {
	@Autowired
	private HomeService homeService;
	
	@RequestMapping("home")
	public String home(HttpServletRequest request,HttpServletResponse response, HttpSession session, Model model){
		//清除分类数据
		ProductVo productVo = (ProductVo) session.getAttribute("productVo");
		if(productVo != null){
			productVo.setCid(null);
		}
		
		//获取所有的一级分类
		List<CategoryIdName> categories = homeService.findAllCategories(); 
		model.addAttribute("categoryList", categories);
		
		String pageSize = Global.PAGE_SIZE;
		//获取热销商品
		List<JiuObject> hotList = homeService.findHotList(pageSize); 
		//处理图片
		for(JiuObject jiuObject : hotList){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("ListHot",hotList);
		//白酒
		List<JiuObject> listB = homeService.findlatestB(pageSize);
		//处理图片
		for(JiuObject jiuObject : listB){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("listB", listB);
		//葡萄酒
		List<JiuObject> listPu = homeService.findlatestPu(pageSize);
		//处理图片
		for(JiuObject jiuObject : listPu){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("listPu", listPu);
		//洋酒
		List<JiuObject> listY = homeService.findlatestY(pageSize);
		//处理图片
		for(JiuObject jiuObject : listY){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("listY", listY);
		//啤酒
		List<JiuObject> listP = homeService.findlatestP(pageSize);
		//处理图片
		for(JiuObject jiuObject : listP){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("listP", listP);
		//黄酒|果酒
		List<JiuObject> listH = homeService.findlatestH(pageSize);
		//处理图片
		for(JiuObject jiuObject : listH){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		model.addAttribute("listH", listH);
		return "home";
	}
	

	

	
	@RequestMapping("index")
	public String index(HttpServletRequest request,HttpServletResponse response, Model model){
		return "index";
	}
	
	@RequestMapping("a")
	public String a(HttpServletRequest request,HttpServletResponse response, Model model){
		return "a";
	}
	
	@RequestMapping("b")
	public String b(HttpServletRequest request,HttpServletResponse response, Model model){
		return "b";
	}
}
